//引入less样式
require('../less/enter.less');

//引入字体
require('../fonts/iconfont.css')


//原生js封装的文档加载事件
document.ready(function () {



    

    //表单验证
    // 声明
    let password = document.querySelector('.password')
    let Text = document.querySelector('.Text')
    let hint = document.querySelector('.hint')
    let btn = document.querySelector('.btn')

    //点击btn验证
    btn.addEventListener('click', function () {
        //判断

        if (until.isPhone(Text.value)) {
            hint.textContent = '手机号输入错误，请稍后重试'
        } else if (!password.value) {
            hint.textContent = '请输入密码'
        } else {
            
            //ajax发送请求
            $http.post('/users/login', { account: Text.value, password: password.value }, function (res) {
                //判断
                if (res.status === 0) {
                    until.showToast('success','登录成功')

                    
                    setTimeout(function(){
                        location.href = "./home.html"
                    },2500)
                } else {
                    until.showToast('fail','登录失败')

                }

                //都正确    
                hint.textContent = ''

            })
        }
    })

    password.addEventListener('keyup', function (e) {
        if (e.keyCode === 13) {
            if (until.isPhone(Text.value)) {
                hint.textContent = '手机号输入错误，请稍后重试'
            } else if (!password.value) {
                hint.textContent = '请输入密码'
            } else {
                //ajax发送请求
                $http.post('/users/login', { account: Text.value, password: password.value }, function (res) {
                    //判断
                    if (res.status === 0) {
                        until.showToast('success','登录成功')

                        setTimeout(function(){

                            //将用户id保存到本地存储中
                            localStorage.setItem('userId',res.data.user.userId)
                            
                            

                            location.href = "./home.html"
                        },2500)
                        
                    } else {
                        until.showToast('fail','登录失败')
                    }

                    //都正确    
                    hint.textContent = ''
                })
            }
        }
    })

})